<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var a = [1, 2, 4]
        var b = [1, 3, 8, 4]
        const newArr = a.concat(b).filter((item, _, arr) => {
            console.log('111',arr.indexOf(item));

            // 原理:
            // 如果某个元素只出现一次 → 它的首次出现位置和最后一次出现位置是相同的
            // 如果某个元素出现多次 → 首次和末次位置不同
            
            return arr.indexOf(item) === arr.lastIndexOf(item)
        })


        /*手写flat*/ 

        const flat = function (arr,deep = 1){
            let result = []

            arr.forEach(item =>{
                if(Array.isArray(item) && deep > 0){

                    result = result.concat(flat(item,deep-1))
                }else{
                    result.push(item)
                }
            })
            return result
        }

        
        // 判断出现次数最多的次数
        const aa = [0, 1, 1,1, 2, 3, 5, 5, 5, 100]
        
        function findAllMostFrequent(arr){
            if(arr.length === 0) return []

            const countMap = new Map();
            for(const item of arr){
                countMap.set(item,(countMap.get(item)||0)+1)
            }
            console.log('countMap',countMap)
            console.log(typeof countMap)
            console.log(countMap instanceof Array)
            console.log('countMap-value',countMap.values())
            console.log('countMap-keys',countMap.keys())
            console.log('type',typeof countMap.keys()) // obj
            console.log(countMap.values() instanceof Array) // false
            const maxCount = Math.max(...countMap.values())
            const result = []
            for(const [value,count] of countMap){
                if(count === maxCount){
                    result.push({value,count})
                }
            }
            console.log('result',result)
            return result
        }

        findAllMostFrequent(aa)

        const getMax = (aa)=>{
            if(arr.length === 0) return []

            const countMap = new Map()
            for(const item of arr){
                countMap.set(item,(countMap.get(item) || 0)+1)
            }
            const maxCount = Math.max(...countMap.values())
            const result = []
            for(const [value,count] of countMap){
                if(count === maxCount){
                    result.push({value,count})
                }
            }

            return result
        }
    </script>
</body>
</html>